from os import popen as figpo
import os
import sys

glist = ["line","pset"]
pavgraphic = -1
display = ""
    
special = ["swap"]  ##function calls not needing assignments like print
c_list = [(255,255,255),(0,0,0),(255,0,0),(0,255,0),(0,0,255)]
# white black red green blue 




def create_p(param_list):
    parameter= ","
    for i in range(0, len(param_list)):
        parameter += param_list[i] + ","
    parameter = parameter[0:-1]
    return parameter

def handle_function_sub(vname, fname, param_list):
    global pavgraphic 
    output = ""
    if len(param_list)>= 1:
        output = vname +"="+" pav"+fname+"("+vname+create_p(param_list)+")"
        if fname == "arrset":
            print(param_list)
            output = vname+"["+str(int(param_list[0])-1)+"] = " +param_list[1] #array[5] = "hello"

    if len(param_list)==0:
        output = vname+" = "+"pav"+fname+"("+vname+")"

    
    return output


def handle_function_fc(vname, fname, param_list):
    output = ""
    if len(param_list)==0:
        output = fname + "("+vname+")"
    if len(param_list)>0:
        output = "pav"+fname+"("+create_p(param_list)[1:]+")"
        if fname in special:
            if fname =="swap":
                output = pavswap(param_list)
    #if fname in glist:
    #    global pavgraphic
    #    pavgraphic = -1

    

    return output


def handle_function_ud(vname, fname, param_list):
    parameter = ""
    for i in range(0, len(param_list)):
        parameter += param_list[i] + ","
    parameter = parameter[:-1]
    output = vname + " = " + fname + "(" + parameter + ")"

    return output


# pavum functions

def pavleft(p, no):
    return p[:no]


def pavint(p):
    return int(p)


def pavstr(p):
    return str(p)

def pavlcase(p):return p.lower()

def pavucase(p):return p.upper()

def pavplus(p,s):
    if type(p) in (float, int):
        if type(s) in (float, int):
            p = p + s
        else:
            p = p + s  # float(s) if you want it easier
        if p == float(int(p)): p = int(p)
    else:
        if type(p) == str: p = p + s  # str(s) if you want it easier
        if type(p) == list:
            if type(s) == tuple:
                p = p + list(s)
            elif type(s) == list:
                p = p + s[:]
            else:
                p = p + [s]
        if type(p) == tuple:
            if type(s) == tuple:
                p = tuple(list(p) + list(s))
            elif type(s) == list:
                p = tuple(list(p) + s[:])
            else:
                p = tuple(list(p) + [s])
    return p




def pavminus(p,s):
    return p-s

def pavtimes(p,s):
   if type(p) in (float,int):
       p = p * s
   if type(p) == "list":
       p = p[:]*s
   else :
       p = p*s

   return p

def pavdivby (p,s):
    return p/s 


def pavarr(p):
    return [p]

def pavarrverse(p):
    return  p[::-1]

def pavarrget(p,s):#set x to 6th item of array p
    return p[s-1]


def pavmid(p, s, x):
    arr = 0
    if type(p) == list or type(p) == tuple: arr = 1
    rt = p[s - 1:x + s - 1]
    if arr and len(rt) == 1: rt = rt[0]
    return rt

def pavarrsort(p):
    return sorted(p,reverse= False)

def pavlineinput(p):
    return raw_input()


def pavshell(c): #function from figarrshell
    ps = []
    global figsysteme
    try:
        figsysteme = 0
        sh = figpo(c)
        ps = sh.read().replace(chr(13) + chr(10),
        chr(10)).replace(chr(13), chr(10)).split(chr(10))
        figsysteme = sh.close()
    except:
        print ("error running arrshell command: " + chr(34) + str(c) + chr(34))

    return ps[:]


def reverse(p):
  str = ""
  for i in p:
    str = i + str
  return str

def pavswap(plist):
    output = ""
    output = plist[0]+","+plist[1]+"="+plist[1]+","+plist[0]
    return output


def pavline(x1,y1,x2,y2,colour):
    import pygame
    global pavgraphic
    global display

    #print pavgraphic

    if int(pavgraphic) == -1 :
        pygame.init()
        display = pygame.display.set_mode((300,300))
        pavgraphic = 1
    if pavgraphic == 1 :
        pass
   


    pygame.draw.line(display,c_list[colour],(x1,y1),(x2,y2),3)

    for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
    pygame.display.update()
   

        
    

def pavpset(x,y,colour):
    import pygame
    global pavgraphic
    global display

    #print pavgraphic

    if int(pavgraphic) == -1 :
        pygame.init()
        display = pygame.display.set_mode((300,300))
        pavgraphic = 1
    if pavgraphic == 1 :
        pass
   
   
    pix_arr = pygame.PixelArray(display)
    pix_arr[x][y] = c_list[colour]

    for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
    pygame.display.update()
    


        
     
